<template>
  <cement-content
    :title="stratumTitle"
    :search="searchData"
    @getData="getStratum"
    @deleteRow="deleteStratum"
    @submitUpdate="submitStratumUpdate"
    @submitSearchModel="submitStratumSearchModel">
  </cement-content>
</template>

<script>
import cementContent from '@/components/cement-content'
import {getStratum, deleteStratum, updateStratum,  getTableHead, getTableData} from '@/api/module/Stratum'
import {get, cloneDeep} from 'lodash'
import {mapGetters, mapMutations} from 'vuex'
import {STRATUM_TITLE, BOREHOLE_SEARCH_DATA, SEPARATOR} from '@/util/constant'

export default {
  name: 'index',
  components: {
    cementContent
  },
  data () {
    return {
      stratumTitle: STRATUM_TITLE,
      searchData: BOREHOLE_SEARCH_DATA
    }
  },
  mounted () {
    this.getStratum()
    this.getData()
  },
  methods: {
    ...mapMutations([
      'changeTableData',
      'changeTotal',
      'closeDialog',
      'changeCurrentPage'
    ]),
    getStratum () {
      let search = cloneDeep(this.searchModelGetters)
      if (get(this.searchModelGetters, 'drillDate')) {
        search['drillDate'] = this.searchModelGetters['drillDate'][0] + SEPARATOR + this.searchModelGetters['drillDate'][1]
      }
      getStratum(this.currentPageGetters - 1, this.pageSizeGetters, this.searchModelGetters).then((res) => {
        this.changeTableData(get(res, 'data.data.content', []))
        this.changeTotal(get(res, 'data.data.totalElements', 0))
      })
    },
    getData(){
      getTableHead().then((res) => {
        console.log(res.data)
      })

    },
    deleteStratum (row) {
      if (get(row, 'wellId')) {
        deleteStratum(row['wellId'])
      }
    },
    submitStratumSearchModel () {
      this.changeCurrentPage(1)
      this.getStratum()
    },
    submitStratumUpdate () {
      if (get(this.updateModelGetters, 'wellId')) {
        updateStratum(get(this.updateModelGetters, 'wellId'), this.updateModelGetters).then(
          this.closeDialog()
        )
      }
    }
  },
  computed: {
    ...mapGetters([
      'pageSizeGetters',
      'currentPageGetters',
      'searchModelGetters',
      'updateModelGetters'
    ])
  }
}
</script>
